<?php
/**
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage http
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* http response data structure : html/xml/other content, headers etc
* @uses urlBean
*/
class httpResponseBean extends dataBean {
    /**
    * constructor alias
    * @access public
    * /
    function httpResponseBean() {
        waklib::needclass('datatree');
        $this->content=&new datatree();
    }/**/

    /**
    * sends response (headers+content) to client/browser
    * @access public
    * @return bool
    */
    function send() {
        $this->sendHeaders();
        return $this->sendContent();
    }
    function sendAll() {
        return $this->send();    
    }
    
    /**
    * sends headers to client/browser
    * @access public
    * @return bool
    */
    function sendHeaders() {
        if ($this->headersSent) return false;
        // sending status line
        header($this->_statusLine());
        // sending additional headers
        foreach ($this->headers as $key => $value)
            header("$key: $value");
        $this->headersSent=true;
        return true;
    }/**/
    
    /**
    * sends response content (html page, etc)
    * may use a template if set
    * else joins current data as a string and echoes it
    * @access public
    * @return bool
    */
    function sendContent() {
        //print_r($this->content);
        // print_r($this->template);
        if (is_object($this->template)) {
            $content=$this->template->render($this->content);
        } else if (is_array($this->content)) {
            // print_r($this->content);
            $content=join('',$this->content);
        } else if (is_string($this->content)) {
            $content=$this->content;
        } else {
            trigger_error("no content ?");
            return false;
        }
        echo $content;
        return true;
    }/**/
    
    /**
    * sets a header to be sent
    * @param string $key
    * @param string $value
    * @access public
    * @return bool
    */
    function setHeader($key,$value) {
        $this->headers[$key]=&$value;
        return true;
    }/**/
    
    /**
    * prepare a cookie to be sent
    * @param
    * @param
    * @todo
    * /
    function cookie() {
        
    }/**/
    
    /**
     * sets all content at once
     * @access public
     * @param string $content
     * @return bool
     */
    function setContent($content) {
        $this->content=array($content);
        return true;
    }
    
    /**
    * Appends some content.
    * @access public
    * @param mixed $content content to insert.
    * @param string $key optional, will use $key as a key for this part
    * @return bool
    */
    function addContent($content,$key=null) {
        if ($key) {
            $this->content[$key]=$value;
        } else {
            $this->content[]=$value;
        }
        return true;
    }/**/
    
    /**
     * gets whole content
     * @access public
     * @param string
     * @return string
     */
    function getContent() {
        return $this->content;
    }/**/
    
    /**
     * Sets template instance for content rendering
     * The template object have to implement a
     * <code>string render(array $data)</code> method
     * @access public
     * @param object $template
     * @return bool
     * /
    function setTemplate(&$template) {
        if (!is_object($template)
        or !method_exists($template,'render'))
            return false;
        $this->template=&$template;
        return true;
    }
    
    /**
    * inserts a new content part
    * @param mixed &$content
    * @param string $key optional
    * @return bool
    * /
    function insertContent(&$content, $key=NULL) {
    
    }
    
    
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
    var $status=200;
    function _status_inputFilter($x) {
        return $this->_toInteger($x);
    }
    /*function _status_set($x) {
        //$this->headers['Status-code']=$x;
        return true;
    }*/
    
    function _status_check($x) {
        return in_array($x,array(
            100,101
            ,200,201,202,203,204,205,206
            ,300,301,302,303,304,305,306,307
            ,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417
            ,500,501,502,503,504,505));
    }
    
    var $_status_reasons=array(
        '100' => 'Continue'
        , '101' => 'Switching Protocols'
        , '200'  => 'OK'
        , '201'  => 'Created'
        , '202'  => 'Accepted'
        , '203'  => 'Non-Authoritative Information'
        , '204'  => 'No Content'
        , '205'  => 'Reset Content'
        , '206'  => 'Partial Content'
        , '300'  => 'Multiple Choices'
        , '301'  => 'Moved Permanently'
        , '302'  => 'Found'
        , '303'  => 'See Other'
        , '304'  => 'Not Modified'
        , '305'  => 'Use Proxy'
        , '307'  => 'Temporary Redirect'
        , '400'  => 'Bad Request'
        , '401'  => 'Unauthorized'
        , '402'  => 'Payment Required'
        , '403'  => 'Forbidden'
        , '404'  => 'Not Found'
        , '405'  => 'Method Not Allowed'
        , '406'  => 'Not Acceptable'
        , '407'  => 'Proxy Authentication Required'
        , '408'  => 'Request Time-out'
        , '409'  => 'Conflict'
        , '410'  => 'Gone'
        , '411'  => 'Length Required'
        , '412'  => 'Precondition Failed'
        , '413'  => 'Request Entity Too Large'
        , '414'  => 'Request-URI Too Large'
        , '415'  => 'Unsupported Media Type'
        , '416'  => 'Requested range not satisfiable'
        , '417'  => 'Expectation Failed'
        , '500'  => 'Internal Server Error'
        , '501'  => 'Not Implemented'
        , '502'  => 'Bad Gateway'
        , '503'  => 'Service Unavailable'
        , '504'  => 'Gateway Time-out'
        , '505'  => 'HTTP Version not supported');
    
    function _statusLine() {
        return 'HTTP/1.1 '.$this->status.' '.$this->_status_reasons[$this->status];
    }
    


    /**
    * @var array additional http response headers, key => value
    */
    var $headers=array(); // http response headers
    /**
    * @var bool have headers been sent yet ?
    */
    var $headersSent=false;
    /**
    * @var array content storage
    */
    var $content=array();
    /**
    * @var string mime type
    */
    var $mime; // mime type
    /**
    * @var array cookies to be sent
    */
    var $cookies=array();
}
?>